import com.chenye.rabbitmq.RabbitTemplateApplication;
import com.chenye.rabbitmq.rabbit.DLX.MessageProvider;
import com.chenye.rabbitmq.rabbit.DLX.QueueEnum;
import com.chenye.rabbitmq.rabbit.HelloSender;
import com.chenye.rabbitmq.rabbit.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by chenye on 2018/4/15.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {RabbitTemplateApplication.class})
public class RabbitmqTemplate {
    @Autowired
    private HelloSender helloSender;
    @Resource(name = "firstRabbitTemplate")
    private RabbitTemplate rabbit;
    @Test
    public void send(){
        for (int i = 0; i <20 ; i++) {
            helloSender.send(i);
        }
    }
    @Test
    public void sendUser(){
        rabbit.convertAndSend("user",new User("chenye",25));
    }
    @Test
    public void send1() {
        String context = "hi, i am message 1";
        System.err.println("Sender : " + context);
        this.rabbit.convertAndSend("exchange", "topic.message", context);
    }
    @Test
    public void send2() {
        String context = "hi, i am messages 2";
        System.err.println("Sender : " + context);
        this.rabbit.convertAndSend("exchange", "topic.messages", context);
    }
    @Test
    public void fanoutSend() {
        String context = "hi, fanout msg ";
        System.out.println("Sender : " + context);
        this.rabbit.convertAndSend("fanoutExchange","", context);
    }

    @Test
    public void dlxMesaageSend(){
        String str = "im dlx message"+new Date();
        rabbit.convertAndSend("direct_ttl_exchange", "ttl_routeKey", str, message -> {
            // 设置延迟毫秒值
            message.getMessageProperties().setExpiration(String.valueOf(3000));
            return message;
        });
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Autowired
    private MessageProvider messageProvider;

    @Test
    public void sendTTLMessage() throws InterruptedException {
        messageProvider.sendMessage("测试延迟消费,写入时间：" + new Date(),
                QueueEnum.MESSAGE_TTL_QUEUE.getExchange(),
                QueueEnum.MESSAGE_TTL_QUEUE.getRouteKey(),
                3000);
        Thread.sleep(10000);
    }


    @Resource(name="secondRabbitTemplate")
    private AmqpTemplate secondRabbitTemplate;
    @Test
    public void testSecondFactory(){
//        this.rabbit.convertAndSend("exchange", "topic.messages", "this is first rabbitmq");
        System.err.println("secondMessage");
//        secondRabbitTemplate.convertAndSend("secondExchange","secondRouteKey","hello second rabbitmq");
        secondRabbitTemplate.convertAndSend("secondQueue","hello second rabbitmq",message -> {
            //将消息设置为持久化，但是这样会降低rabbitmq的吞吐性能
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            message.getMessageProperties().setPriority(1);
            return message;
        });

    }

    @Test
    public void testAck() throws InterruptedException {
        String context = "hi, ack msg ";
        System.out.println("Sender : " + context);
        this.rabbit.convertAndSend(QueueEnum.MESSAGE_QUEUE.getExchange(),QueueEnum.MESSAGE_QUEUE.getRouteKey(), context);
        Thread.sleep(1000);
    }
}
